package sunwin.yoms.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.domain.system.FireWallDomain;
import sunwin.yoms.service.system.FireWallService;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsAjaxAnswer;
import sunwin.yoms.web.comm.YomsController;
import sunwin.yoms.web.comm.resolverArg.YomsArrayParam;

import java.util.List;

/**
 * Created by watson on 2015/10/30.
 */
@Controller
public class FireWallController {
    @Autowired
    private FireWallService fireWallService;
    private final String FindFire = "system/fireWall.find";
    private final String CreateFire = "system/fireWall.create";
    private final String EditFire = "system/fireWall.edit";
    private final String DelFire = "system/fireWall.delete";

    /**
     * 查找防火墙列表
     */
    @RequestMapping(value = R.FindFireWall, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.FindFireWall, viewName = FindFire)
    public ModelAndView showFireList(FireWallDomain fireWallDomain,
                                     ModelMap modelMap) throws YomsException {
        modelMap.addAttribute("fireWallDomain", fireWallDomain);
        List<FireWallDomain> domains = fireWallService.getFireList(fireWallDomain);
        ModelAndView mav = new ModelAndView();
        mav.addObject("domains", domains);
        return mav;
    }

    /**
     * 增加防火墙访问页面
     */
    @RequestMapping(value = R.CreateFireWall, method = RequestMethod.GET)
    @YomsController(viewName = CreateFire)
    public void addFireWall() {

    }

    /**
     * 增加防火墙
     */
    @RequestMapping(value = R.CreateFireWall, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.CreateFireWall, viewName = CreateFire)
    @ResponseBody
    public YomsAjaxAnswer addFireWall2(FireWallDomain fireWallDomain) throws YomsException {
        YomsAjaxAnswer answer = new YomsAjaxAnswer();
        answer.setSuccess(true);
        fireWallService.addFireWall(fireWallDomain);
        return answer;
    }

    /**
     * 访问编辑防火墙
     */
    @RequestMapping(value = R.EditFireWall, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.EditFireWall, viewName = EditFire)
    public ModelAndView editFireWall(int id) throws YomsException {
        FireWallDomain domain = fireWallService.getFireWallById(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("fireDomain", domain);
        return mav;
    }

    /**
     * 编辑防火墙
     */
    @RequestMapping(value = R.EditFireWall, method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @YomsController(functionNo = YomsFunction.EditFireWall, viewName = EditFire)
    @ResponseBody
    public YomsAjaxAnswer editFireWall2(FireWallDomain fireWallDomain) throws YomsException {
        YomsAjaxAnswer answer = new YomsAjaxAnswer();
        answer.setSuccess(true);
        fireWallService.editFireWall(fireWallDomain);
        return answer;
    }

    /**
     * 删除防火墙
     */
    @RequestMapping(value = R.DelFireWall, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.DelFireWall, viewName = DelFire)
    @ResponseBody
    public YomsAjaxAnswer delFire(@YomsArrayParam List<Integer> ids) throws YomsException {
        if (ids != null) {
            for (int id : ids) {
                fireWallService.deleteFire(id);
            }
        }
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }
}
